package com.project.service;

import com.project.common.utils.MyPageHelper;
import com.project.dao.AttendExamMapper;
import com.project.entity.AnswerRecord;
import com.project.entity.AttendExam;
import com.project.myBeans.ExerciseDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class AttendExamServiceImpl extends BaseService implements AttendExamService {

    @Autowired
    AttendExamMapper attendExamMapper;
    @Autowired
    AnswerRecordService answerRecordService;

    @Override
    public List<AttendExam> findByCondition(AttendExam attendExam) {
        MyPageHelper.setPageParam(attendExam);
        return attendExamMapper.selectByCondition(attendExam);
    }

    @Override
    public void addBatch(Integer examid,List<Integer> studentidList){
        for(Integer studentid : studentidList){
            AttendExam attendExam=new AttendExam();
           attendExam.setExamid(examid);
           attendExam.setStudentid(studentid);
           attendExamMapper.insertSelective(attendExam);
        }
    }

    @Override
    public void add(AttendExam attendExam) {
        attendExamMapper.insert(attendExam);
    }

    @Override
    public void submitAllByExamId(int examid, Timestamp endtime)
    {
        AttendExam attendExam = new AttendExam();
        attendExam.setExamid(examid);
        List<AttendExam> attendExams = attendExamMapper.selectByCondition(attendExam);
        for (AttendExam tattendExam:attendExams)
        {
            if(tattendExam.getEndtime()==null)
            {
                tattendExam.setEndtime(endtime);
                attendExamMapper.updateByPrimaryKeySelective(tattendExam);
            }
        }
    }

    @Override
    public void submitExam(AttendExam attendExam, List<ExerciseDetail>exerciseDetails)
    {
        AttendExam attendExam1 = attendExamMapper.selectByCondition(attendExam).get(0);
        if(attendExam1!=null)
        {
            Timestamp curtime = new Timestamp(System.currentTimeMillis());
            attendExam1.setEndtime(curtime);
            attendExamMapper.updateByPrimaryKeySelective(attendExam1);
            answerRecordService.saveExamAnswer(exerciseDetails,attendExam.getExamid(),attendExam.getStudentid());
        }

    }

    @Override
    public AttendExam beginExam(AttendExam attendExam)
    {
        List<AttendExam> attendExams = attendExamMapper.selectByCondition(attendExam);
        if(attendExams!=null)
        {
            AttendExam attendExam1 = attendExamMapper.selectByCondition(attendExam).get(0);
            if(attendExam1.getEndtime()==null)
            {
                Timestamp curtime = new Timestamp(System.currentTimeMillis());
                attendExam1.setStarttime(curtime);
                attendExamMapper.updateByPrimaryKeySelective(attendExam1);
            }
            return attendExam1;
        }else
        {
            return null;
        }


    }
}
